/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sgt.supermarket.controller.product;

import java.io.IOException;
import java.text.ParseException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import sgt.supermarket.controller.XServlet;
import sgt.supermarket.entity.Employee;
import sgt.supermarket.entity.Product;
import sgt.supermarket.entity.ProductBlock;
import sgt.supermarket.hibernatedao.HibernateDAOFactory;
import sgt.supermarket.util.Converter;
import sgt.supermarket.util.ServletExtensions;

/**
 *
 * @author Administrator
 */
public class ViewPurchase extends XServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, ParseException {
        HibernateDAOFactory factory = new HibernateDAOFactory();
        Product product = factory.getProductDAO().getById(ServletExtensions.getLongParam(request, "pid"));
        if (request.getMethod().equalsIgnoreCase("POST") && request.getParameter("add") != null) {
            double cost = ServletExtensions.getDoubleParam(request, "price");
            int quantity = ServletExtensions.getIntParam(request, "quantity");
            Employee employee = factory.getEmployeeDAO().getByUsername(request.getRemoteUser());
            String purchaseDate = request.getParameter("purchaseDate") + "/" + request.getParameter("purchaseDate-mm") + "/" + request.getParameter("purchaseDate-dd");

            ProductBlock block = new ProductBlock(employee, cost, Converter.toDate(purchaseDate, "yyyy/M/d"), quantity, quantity);
            product.addBlock(block);
            factory.getProductDAO().save(product);
        }
        request.setAttribute("title", "View purchases: " + product.getName());
        request.setAttribute("product", product);
        request.getRequestDispatcher("/product.viewPurchase.tiles").forward(request, response);
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (ParseException ex) {
            Logger.getLogger(ViewPurchase.class.getName()).log(Level.SEVERE, null, ex);
            throw new ServletException(ex);
        }
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (ParseException ex) {
            Logger.getLogger(ViewPurchase.class.getName()).log(Level.SEVERE, null, ex);
            throw new ServletException(ex);
        }
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
